package com.wtwd.wtwddevice.listener.appMsg;


import com.alibaba.fastjson.JSONObject;
import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.AnswerStateEnum;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.model.BloodPressureWarning;
import com.wtwd.device.model.RemoteOperateTerminal;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.AppProtocol;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import com.wtwd.wtwddevice.model.entity.SetHeartRateWarning;
import com.wtwd.wtwddevice.mq.PushAppMsgService;
import com.wtwd.wtwddevice.service.IDeviceSendService;
import com.wtwd.wtwddevice.service.IDeviceService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@MsgType(MsgTypeEnum.APP_SET_REMOTE_OPERATE_TERMINAL)
@Component
public class appSetRemoteOperateTerminalHandlerImpl implements IMsgHandler{


    @Resource
    private IDeviceSendService deviceSendService;

    @Resource
    private PushAppMsgService msgService;

    @Override
    public void handleMsg(MessageBase messageBase) {
        AppProtocol appProtocol = messageBase.getAppProtocol();
        JSONObject json = (JSONObject) appProtocol.getData();
        RemoteOperateTerminal data = json.toJavaObject(RemoteOperateTerminal.class);
        WTProtocol wtProtocol = WTProtocol.builder()
                .imei(appProtocol.getImei())
                .cmdName(MsgTypeEnum.REMOTE_OPERATE_TERMINAL.getCmdName())
                .body(data.serialization())
                .build();
        if (!deviceSendService.sendMsgDevice(wtProtocol)){//如果为fase直接告知app端设置失败
                AppProtocol protocol = AppProtocol.builder()
                        .imei(wtProtocol.getImei())
                        .cmdName(MsgTypeEnum.APP_SET_REMOTE_OPERATE_TERMINAL.getCmdName())
                        .state(AnswerStateEnum.ABNORMAL.getState())
                        .data(null)
                        .build();
                msgService.pushToDevice(messageBase.getForm(),
                        MessageBase.builder()
                                .appProtocol(protocol)
                                .build()
                );
        }
    }

    public static void main(String[] args) {
        RemoteOperateTerminal data = new RemoteOperateTerminal();
        data.setReset(1);
        data.setRestart(0);
        String s = JSONObject.toJSONString(data);
        System.out.println(s);
    }
}
